package com.wang.controller_wifi.manager;

import android.content.Context;
import android.os.Message;

public class HandleAction {
	
	public volatile boolean isInterrupt;
	private SocketController controller;
	private WifiManager manager;
	
	public HandleAction(Context ctx, WifiManager wifiManager) {
		this.manager = wifiManager;
		controller = new SocketController();
	}

	public void handle(Message msg){
		isInterrupt = false;
		switch (msg.what) {
		case Constant.ACTION_CONNECT:
			boolean res = controller.connect();
			if(res){
				manager.notifyMessage(Constant.NOTIFY_CONNECT_SUCCESS);
			} else {
				manager.notifyMessage(Constant.NOTIFY_CONNECT_FAIL);
			}
			break;
		case Constant.ACTION_CMD:
			String cmd = (String) msg.obj;
			if("i".equals(cmd) || "j".equals(cmd)){
				while(!isInterrupt){
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					controller.write(cmd);
				}
			} else {
				controller.write(cmd);
			}
			break;
		}
	}

}
